import { _decorator, Button, Component, director, Label, Node, RichText, sys, Toggle, ToggleComponent } from 'cc';
import { AudioMgr } from './AudioMgr';
import { LocalStorageKey } from './ConstValue';
const { ccclass, property } = _decorator;

@ccclass('GameHomeController')
export class GameHomeController extends Component {

    @property(Node)
    private settingLayout: Node = null
    @property(Node)
    private mainLayout: Node = null
    @property(Node)
    private hitoreScoreNode: Node = null

    @property(Button)
    private newGameBtn: Button = null
    @property(Button)
    private settingBtn: Button = null
    @property(Button)
    private saveSettingBtn: Button = null
    @property(RichText)
    private titleLable: RichText = null

    @property(Toggle)
    private backgroundMusicToggle: Toggle = null
    @property(Toggle)
    private soundEffectsToggle: Toggle = null

    start() {
        this.settingBtn.node.on(Button.EventType.CLICK, this.clickSetting, this);
        this.newGameBtn.node.on(Button.EventType.CLICK, this.clickNewGame, this);
        this.saveSettingBtn.node.on(Button.EventType.CLICK, this.saveSettingClick, this);

        this.backgroundMusicToggle.node.on(Toggle.EventType.TOGGLE, this.backgroundMusicChange, this);
        this.soundEffectsToggle.node.on(Toggle.EventType.TOGGLE, this.soundEffectsChange, this);

        let playBackgroundMusic = sys.localStorage.getItem(LocalStorageKey.PLAYBAKGROUNDMUSIC)
        if (playBackgroundMusic) {
            this.backgroundMusicToggle.isChecked = playBackgroundMusic == "y"
            AudioMgr.inst.setPlayBackground(playBackgroundMusic == "y")
        } else {
            AudioMgr.inst.setPlayBackground(true)
        }

        let playSoundEffects = sys.localStorage.getItem(LocalStorageKey.PLAYSOUNDEFFECTS)
        if (playSoundEffects) {
            this.soundEffectsToggle.isChecked = playSoundEffects == "y"
            AudioMgr.inst.setSoundEffectsCheck(playSoundEffects == "y")
        } else {
            AudioMgr.inst.setSoundEffectsCheck(true)
        }
        // director.preloadScene("GameMainScene")
        AudioMgr.inst.play("musics/worldscenebgm", 1, true)
        this.showHistoryScore()
    }


    update(deltaTime: number) {

    }

    private backgroundMusicChange(event: Toggle) {
        AudioMgr.inst.setPlayBackground(event.isChecked)
        sys.localStorage.setItem(LocalStorageKey.PLAYBAKGROUNDMUSIC, event.isChecked ? "y" : "n")
        if (event.isChecked) {
            AudioMgr.inst.play("musics/worldscenebgm", 1, true)
        } else {
            AudioMgr.inst.stop()
        }
    }

    private soundEffectsChange(event: Toggle) {
        AudioMgr.inst.setSoundEffectsCheck(event.isChecked)
        sys.localStorage.setItem(LocalStorageKey.PLAYSOUNDEFFECTS, event.isChecked ? "y" : "n")
    }

    private showHistoryScore() {
        let score = sys.localStorage.getItem(LocalStorageKey.SCORE)
        if (score) {
            let lableNode = this.hitoreScoreNode.children[1]
            let lable = lableNode.getComponent(Label)
            lable.string = score
        }
        this.hitoreScoreNode.active = score != null
        // this.titleLable.node.active = score == null
    }

    private saveSettingClick() {
        AudioMgr.inst.playOneShot("musics/click.bubble")
        this.titleLable.string = "<size=72><color=#00ff00>Sweet Fruit</color></size>" + "\n  " + "<size=69><color=#4E71F3> <outline color=#6F9FFF width=2>Battle</outline></color> </size>"
        this.settingLayout.active = false
        this.mainLayout.active = true
    }

    private clickSetting() {
        AudioMgr.inst.playOneShot("musics/click.bubble")
        this.titleLable.string = "<size=72><color=#00ff00>Setting</color></size>"
        this.settingLayout.active = true
        this.mainLayout.active = false
    }

    private clickNewGame() {
        AudioMgr.inst.playOneShot("musics/click.bubble")
        window.globalParam = {
            sceneName: "GameMainScene"
        };
        director.loadScene("LoadingScene")
    }

    private clickStore() {
        AudioMgr.inst.playOneShot("musics/click.bubble")

    }
}


